package com.leyou.item.service.controller;

import com.leyou.common.bean.Result;
import com.leyou.item.inter.dto.CategoryUpdateDTO;
import com.leyou.item.inter.pojo.Category;
import com.leyou.item.service.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqq
 * @ClassName CategoryController
 * @Description: 商品类目接口
 * @date 2020/2/23-17:09
 */
@Api(value = "CategoryController",description = "商品类目接口")
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "根据parentId查询子类目", notes = "根据parentId查询子类目")
    @GetMapping("list/{pid}")
    public Result<List<Category>> queryCategoriesByPid(@ApiParam(name = "pid",value = "父类目id",required = true) @PathVariable(value = "pid") Long pid) {
        return this.categoryService.queryCategoriesByPid(pid);
    }

    @GetMapping("bid/{bid}")
    @ApiOperation(value = "根据品牌id查询商品分类", notes = "根据品牌id查询商品分类")
    public Result<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        return this.categoryService.queryByBrandId(bid);
    }

    @PostMapping("names")
    @ApiOperation(value = "根据商品分类id(可以多个id查询)，查询商品分类名称", notes = "根据商品分类id(可以多个id查询)，查询商品分类名称")
    public Result<List<String>> queryNamesByIds(@ApiParam(name = "ids",value = "商品分类id集合",required = true)  @RequestBody @RequestParam("ids")List<Long> ids){
        return this.categoryService.queryNamesByIds(ids);
    }

    @GetMapping("lower/list")
    @ApiOperation(value = "首页按层级查询所有类目信息", notes = "首页按层级查询所有类目信息")
    public Result<List<Category>> queryListByPid() {
        return this.categoryService.queryListByCid();
    }

    @GetMapping("all/level/{id}")
    @ApiOperation(value = "根据3级分类id，查询1~3级的分类", notes = "根据3级分类id，查询1~3级的分类")
    public Result<List<Category>> queryAllByCid3(@ApiParam(name = "id",value = "三级类目id",required = true) @PathVariable("id") Long id){
        return this.categoryService.queryAllByCid3(id);
    }

    @PostMapping("update")
    @ApiOperation(value = "新增类目", notes = "新增类目")
    public Result addCategory(@RequestBody CategoryUpdateDTO dto){
        return this.categoryService.updateCategory(dto);
    }

    @PutMapping("update")
    @ApiOperation(value = "修改类目", notes = "修改类目")
    public Result updateCategory(@RequestBody CategoryUpdateDTO dto){
        return this.categoryService.updateCategory(dto);
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除类目", notes = "删除类目")
    public Result deleteCategory(@ApiParam(name = "id",value = "类目id",required = true) @PathVariable("id") Long id){
        return this.categoryService.deleteCategory(id);
    }

    @GetMapping("cid/{id}")
    @ApiOperation(value = "根据主键查询", notes = "根据主键查询")
    public Result<Category> queryCategoryById(@ApiParam(name = "id",value = "类目id",required = true) @PathVariable("id") Long id){
        return this.categoryService.queryCategoryById(id);
    }

}
